﻿Public Class 用户管理

    '关闭窗体
    Private Sub 用户管理_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        AccountMFM = Nothing
    End Sub

    '添加用户
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        '变量赋值
        Dim na As String = Trim(TextBox1.Text)
        Dim pw As String = Trim(TextBox2.Text)
        Dim p2 As String = Trim(TextBox3.Text)
        Dim c1 As Boolean = CheckBox6.Checked
        Dim c2 As Boolean = CheckBox5.Checked

        '空值检测
        If na = "" Then MsgBox("请输入用户名！") : TextBox1.Focus() : Exit Sub
        If pw = "" Then MsgBox("请输入密码！") : TextBox2.Focus() : Exit Sub
        If p2 = "" Then MsgBox("请再输入一次密码！") : TextBox3.Focus() : Exit Sub

        If pw <> p2 Then MsgBox("两次输入的密码不一致，请重新输入！") : TextBox2.Focus() : Exit Sub

        If (Not c1) And (Not c2) Then MsgBox("两个权限必需选择一个，请选择！") : CheckBox6.Focus() : Exit Sub

        '己存在检测
        Dim str1 As String = "select 用户名 from 用户表 where 用户名 ='" + na + "'"
        Dim dst As DataTable = SQLtoDataSet(str1).Tables(0)
        If dst.Rows.Count > 0 Then MsgBox("用户名'" + na + "'己存在，请重新输入！") : TextBox1.Focus() : Exit Sub


        '添加写入
        Dim sqlstr As String = "insert into 用户表(用户名,密码,查看权限,修改权限,户管权限) values("
        sqlstr += "'" + na + "',"
        sqlstr += "'" + pw + "',"
        sqlstr += c1.ToString + ","
        sqlstr += c2.ToString + ","
        sqlstr += "False)"
        SQLupdata(sqlstr)

        MsgBox("添加用户'" + na + "'操作完成！")

        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        CheckBox6.Checked = False
        CheckBox5.Checked = False
        TextBox1.Focus()

    End Sub

    '选择P2
    Private Sub TabControl1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.Click
        If TabControl1.SelectedIndex = 1 Then
            TextBox1.Focus()
        End If
        If TabControl1.SelectedIndex = 0 Then
            listNa()
        End If
    End Sub

    '窗体加载
    Private Sub 用户管理_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        listNa()

    End Sub

    '加载用户列表
    Private Sub listNa()
        Dim sqlstr As String = "select 用户名 from 用户表"
        Dim dst As DataTable = SQLtoDataSet(sqlstr).Tables(0)
        ListBox1.Items.Clear()
        If dst.Rows.Count > 0 Then
            For i = 0 To dst.Rows.Count - 1
                If dst.Rows(i).Item("用户名").ToString <> "admin" Then
                    ListBox1.Items.Add(dst.Rows(i).Item("用户名").ToString)
                End If
            Next
        End If
    End Sub

    '选择框只读
    Private Sub CheckBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.Click
        If Button1.Text = "修改" Then CheckBox1.Checked = Not CheckBox1.Checked
    End Sub
    Private Sub CheckBox2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox2.Click
        If Button1.Text = "修改" Then CheckBox2.Checked = Not CheckBox2.Checked
    End Sub
    Private Sub CheckBox3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox3.Click
        CheckBox3.Checked = Not CheckBox3.Checked
    End Sub

    '选择用户
    Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
        If ListBox1.SelectedItems.Count > 0 Then
            Dim sqstr As String = "select * from 用户表 where 用户名 ='" + ListBox1.SelectedItem.ToString + "'"
            Dim dst As DataTable = SQLtoDataSet(sqstr).Tables(0)
            If dst.Rows.Count > 0 Then
                CheckBox1.Checked = dst.Rows(0).Item("查看权限")
                CheckBox2.Checked = dst.Rows(0).Item("修改权限")
                CheckBox3.Checked = dst.Rows(0).Item("户管权限")
            Else
                CheckBox1.Checked = False
                CheckBox2.Checked = False
                CheckBox3.Checked = False
            End If

        End If
    End Sub

    '右键菜单
    Private Sub ListBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If ListBox1.SelectedItems.Count = 0 Then
                ContextMenuStrip1.Visible = False
            Else
                ContextMenuStrip1.Visible = True
            End If
        End If
 
    End Sub

    '修改完成
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "修改" Then
            If ListBox1.SelectedItems.Count > 0 Then
                Button1.Text = "确定"
                ListBox1.Enabled = False
                CheckBox1.ForeColor = Color.Blue
                CheckBox2.ForeColor = Color.Blue
            End If
        Else
            Button1.Text = "修改"
            ListBox1.Enabled = True

            Dim strq As String = "update 用户表 set 查看权限 = " + CheckBox1.Checked.ToString + ", 修改权限 = " + CheckBox2.Checked.ToString + " where 用户名 ='" + ListBox1.SelectedItem.ToString + "'"
            SQLupdata(strq)
            CheckBox1.ForeColor = Color.Black
            CheckBox2.ForeColor = Color.Black
        End If
    End Sub

    '删除用户
    Private Sub 删除ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem.Click

        If ListBox1.SelectedItems.Count > 0 Then

            Dim s = MessageBox.Show("你确定要删除用户'" + ListBox1.SelectedItem.ToString + "'吗？", "删除用户", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk)
            If s = Windows.Forms.DialogResult.Yes Then

                Dim sqlstr As String = "delete from 用户表 where 用户名 = '" + ListBox1.SelectedItem.ToString + "'"
                SQLupdata(sqlstr)
                ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
            End If
        End If
    End Sub

End Class